<?php

namespace app\store\model\store;

use app\common\model\store\ShopActivation as ShopActivationModel;
use Lvht\GeoHash;

/**
 * 商家门店模型
 * Class Shop
 * @package app\store\model\store
 */
class ShopActivation extends ShopActivationModel
{
    /**
     * 获取列表数据
     * @param null $status
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($shop_id)
    {
        $where = [];
        $shop_id != 0 && $where = ['shop_id'=>$shop_id];
        return $this->where('is_delete','=',0)
            ->where($where)
            ->order(['create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => \request()->request()
            ]);
    }


    /**
     * 新增记录
     * @param $data
     * @return bool
     * @throws \Exception
     */
    public function add($data)
    {
        if (!$this->validateForm($data)) {
            return false;
        }
        return $this->allowField(true)->save($this->createData($data));
    }

    /**
     * 编辑记录
     * @param $data
     * @return false|int
     */
    public function edit($data)
    {
        if (!$this->validateForm($data)) {
            return false;
        }
        return $this->allowField(true)->save($this->createData($data)) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 创建数据
     * @param array $data
     * @return array
     */
    private function createData($data)
    {
        $data['wxapp_id'] = self::$wxapp_id;
        return $data;
    }

    /**
     * 表单验证
     * @param $data
     * @return bool
     */
    private function validateForm($data)
    {
        return true;
    }

    /**
     * 获取指定日期新增激活用户数量
     * @param $days
     * @param $daye
     */
    public function getActivationTotalDay($days, $daye){
        $days = strtotime($days." 00:00:00");
        $daye = strtotime($daye." 23:59:59");
        $condition = $days .','. $daye;
        return $this->where('is_delete','=',0)
            ->where('create_time','between',$condition)
            ->count();
    }
}